package com.system.post.entity.dto;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import com.system.post.entity.Post;


public class PostQueryDTO {
	private String postName;

	public String getPostName() {
		return postName;
	}

	public void setPostName(String postName) {
		this.postName = postName;
	}

	public static Specification<Post> getSpecification(PostQueryDTO userQueryDTO){
		Specification<Post> spec = new Specification<Post>() {
			@Override
			public Predicate toPredicate(Root<Post> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				// TODO Auto-generated method stub
				List<Predicate> list = new ArrayList<Predicate>();
				if(null !=userQueryDTO &&!StringUtils.isEmpty(userQueryDTO.getPostName())) {
					Predicate p1 = cb.like(root.get("postName").as(String.class), "%"+userQueryDTO.getPostName()+"%");
					list.add(p1);
				}
				Predicate[] p = new Predicate[list.size()];
				return cb.and(list.toArray(p));
			}
		};
		return spec;
	}
}
